import requests
from django.shortcuts import render
from django.views.generic import View
from django.contrib.auth.mixins import LoginRequiredMixin
from django.http import HttpResponse
from django.views.generic.edit import UpdateView
from .forms import Banji_Xueyuan_Upadd_Form
from .models import *
from django.shortcuts import get_object_or_404
from django.shortcuts import redirect
from django.contrib import messages
import datetime



class Xueyuan_Jinban_banji(LoginRequiredMixin, UpdateView):
    model = Xueyuan_zongbiao
    form_class = Banji_Xueyuan_Upadd_Form
    template_name = 'banji/banji_xueyuan_add.html'
    bigtitle='学员进班'

    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        context['banji'] = Banji.objects.all()
        context['bigtitle'] = self.bigtitle
        self.object = self.get_object()
        return context

class Xueyuan_Jinban_banji_add_form(forms.ModelForm):
    class Meta:
        model = Banji
        fields = ['name',]

class Xueyuan_Jinban_banji_add(LoginRequiredMixin, View):
    def get(self, request, *args, **kwargs):
        xueyuan_id = self.kwargs.get('xueyuan_id')
        jinban_id = self.kwargs.get('jinban_id')
        banji = get_object_or_404(Banji, pk=jinban_id)
        xueyuan = get_object_or_404(Xueyuan_zongbiao, pk=xueyuan_id)
        html=f"""
        
        <html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>错误</title>
<link href="https://cdn.staticfile.org/twitter-bootstrap/5.0.2/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
<h1>学员已经在班级中</h1>

<a type="button" class="btn btn-primary" href="/xueyuan/{ xueyuan_id }/jinban/">点击返回</a>

<script src="https://cdn.staticfile.org/twitter-bootstrap/5.0.2/js/bootstrap.bundle.min.js"></script>
</body>
</html>
        
        """
        if xueyuan in banji.xueyuan.all():
            return HttpResponse(html)
        else:

            banji_form = Xueyuan_Jinban_banji_add_form(instance=banji)
            fanhui = {
                'banji_form': banji_form,
                'banji': banji,
                'xueyuan': xueyuan
            }
            return render(request, 'Xueyuan_Jinban_banji_add.html', fanhui)

    def post(self, request, *args, **kwargs):

        xueyuan_id = self.kwargs.get('xueyuan_id')
        jinban_id = self.kwargs.get('jinban_id')
        xueyuan = get_object_or_404(Xueyuan_zongbiao, pk=xueyuan_id)
        banji = get_object_or_404(Banji, pk=jinban_id)
        banji_form = Xueyuan_Jinban_banji_add_form(request.POST, instance=banji)

        if banji_form.is_valid():
            try:
                banji.xueyuan.add(xueyuan)
                messages.success(request, '学员添加成功')
                if xueyuan.xueyuan_jinbantime is None:
                    xueyuan.xueyuan_jinbantime = datetime.now()
                    xueyuan.save()
                return redirect('Xueyuan_detail', pk=xueyuan_id)
            except Exception as e:
                messages.error(request, '学员添加失败')
                return redirect('Xueyuan_detail', pk=xueyuan_id)
        else:
            return render(request, 'Xueyuan_Jinban_banji_add.html', {
                'banji_form': banji_form,
                'banji': banji,
                'xueyuan': xueyuan
            })